package InnerGameParts;

import java.awt.Graphics2D;
//import java.awt.Image;
//import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.Random;

import javax.imageio.ImageIO;

public class Wind  implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 220207152875796425L;
	private final static String WIND_IMAGE_PATH =  "windImage.png";
	private final static int CHANCE_IN_X_WHERE_WIND_WILL_CHANGE = 200;

	private int mDirection;
	private int mWindStrenght;

	private int mChangeDirection;
	private int mChangeAmount;

	private Random mRandom;
	//private BufferedImage mWindImage;

	public final static int GRAPHICS_WIDTH =200; 
	public final static int GRAPHICS_HEIGHT  = 200;

	public Wind() throws IOException{

		//Image myImage = Toolkit.getDefaultToolkit().createImage(WIND_IMAGE_PATH);
		//mWindImage = ImageIO.read( new File(WIND_IMAGE_PATH));
		mRandom = new Random();
		mDirection = 0;
		mWindStrenght = 10;
		changeWind();
	}

	public void windUpdate(){


		if(mChangeAmount > 0){

			mChangeAmount--;
			mDirection = inc(mDirection, mChangeDirection);

		} else if( 0 == mRandom.nextInt(CHANCE_IN_X_WHERE_WIND_WILL_CHANGE)){
			changeWind();
		}






	}



	private void changeWind(){



		int newDirection = mRandom.nextInt(50);

		mChangeAmount =  smallestAngelDiff(mDirection, newDirection);

		if(mDirection - mChangeAmount - newDirection != 0){

			mChangeDirection = findDirection(mDirection,newDirection, false);

		} else{

			mChangeDirection = findDirection(mDirection,newDirection, true);

		}


	}

	
	public int findDirection( final int oldDirection , final int newDirection ,final boolean doesNotOver360Line){
		
		
		if( (oldDirection < newDirection && doesNotOver360Line)    ){
			
			return 1; 
			
		} else{
			
			
			
			return -1; 
			
		}
		
		
		
		
		
		
	}
	
	/*
	 * Finds the smallest difference between the two anlges regruadless if its reflevies of not 
	 */
	public int smallestAngelDiff(final int angleA, final int angleB){

		int diffA = Math.abs(angleA - angleB); 
		int diffB = Math.abs(angleA - angleB + 360);

		if(diffA > diffB){

			return diffB;

		} else{

			return diffA;

		}

	}

	private int inc(int value , int inc){


		value += inc;

		if(value >= 360){

			return value -360;

		}

		if( value < 0){

			return value + 360;

		}

		return value;


	}

	public double windPower(int shipDirection){

		// TODO need refining
		int d1 = Math.abs(mDirection - shipDirection);
		int d2 = Math.abs(mDirection + 360 - shipDirection);
		int out;
		if( d1 < d2){

			out =  (360 - d1) / 72;

		}else{

			out = (360 - d2)/ 144;
		}

		return out;


	}

	public int getWindMoveX(){

		return  mWindStrenght * (int)Math.sin(Math.toRadians(mDirection));

	}

	public int getWindMoveY(){

		return  mWindStrenght *(int)Math.cos(Math.toRadians(mDirection));
		//return 0;

	}

	public int getDirection(){

		return mDirection;
	}

	/*public BufferedImage renderWind(){

		BufferedImage renderImage = new BufferedImage(mWindImage.getWidth(),	mWindImage.getHeight(),
				BufferedImage.TYPE_INT_ARGB); 

		Graphics2D wG = (Graphics2D) renderImage.getGraphics();
		wG.rotate( Math.toRadians(mDirection), mWindImage.getWidth() / 2, mWindImage.getHeight()/2);//, mWindImage.getWidth() /2, mWindImage.getHeight() /2);


		wG.drawImage(mWindImage,0,0, null);




		return renderImage;

	}*/

}
